package com.gouaixin.bee.beeframework2.tool;

import android.content.Context;

import java.util.List;

import cn.bmob.im.BmobChatManager;
import cn.bmob.im.bean.BmobChatUser;
import cn.bmob.im.bean.BmobMsg;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Administrator on 2015/3/24.
 */
public class ImUtils {
    static BmobChatManager mBmobChatManager;
    static BmobChatUser targetUser;
    static String targetId="";
    static String targetName="";
    static String msg="聊天消息测试";
    static Context context;

    //用户name转对象id
    public static String name2id(Context mcontext,String name){
       context=mcontext;

        mBmobChatManager= BmobChatManager.getInstance(context);

        BmobQuery<BmobChatUser> query = new BmobQuery<BmobChatUser>();
        query.addWhereEqualTo("username",name);
        query.findObjects(context, new FindListener<BmobChatUser>() {
            @Override
            public void onSuccess(List<BmobChatUser> object) {
                mLog.e("查询成功：共" + object.size() + "条数据。" + object.toString());
                targetUser = object.get(0);
                targetId = targetUser.getObjectId();
            }

            @Override
            public void onError(int code, String msg) {
                mLog.e("查询失败：" + msg);
            }
        });



        return targetId;
    };

    //对象id转用户name
    public static String id2name(Context mcontext,String id){
        context=mcontext;


        mBmobChatManager= BmobChatManager.getInstance(context);

        BmobQuery<BmobChatUser> query = new BmobQuery<BmobChatUser>();
        query.addWhereEqualTo("objectId",id);
        query.findObjects(context, new FindListener<BmobChatUser>() {
            @Override
            public void onSuccess(List<BmobChatUser> object) {
                mLog.e("查询成功：共" + object.size() + "条数据。" + object.toString());
                targetUser = object.get(0);
                targetName= targetUser.getUsername();
            }

            @Override
            public void onError(int code, String msg) {
                mLog.e("查询失败：" + msg);
            }
        });



        return targetId;
    };


    //给目标id发送字符串
    public static void sendMsg(Context mcontext,String td,String msg0){
        context=mcontext;
        msg=msg0;

        mBmobChatManager= BmobChatManager.getInstance(context);

        BmobQuery<BmobChatUser> query = new BmobQuery<BmobChatUser>();
        query.addWhereEqualTo("objectId",td);
        query.findObjects(context, new FindListener<BmobChatUser>() {
            @Override
            public void onSuccess(List<BmobChatUser> object) {
                mLog.e("查询成功：共" + object.size() + "条数据。" + object.toString());
                targetUser = object.get(0);
                targetName= targetUser.getUsername();


                // 组装BmobMessage对象
                BmobMsg message = BmobMsg.createTextSendMsg(context, targetId, msg);
                // 默认发送完成，将数据保存到本地消息表和最近会话表中
                mBmobChatManager.sendTextMessage(targetUser, message);
            }

            @Override
            public void onError(int code, String msg) {
                mLog.e("查询失败：" + msg);
            }
        });


    };




}
